

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.ico">
  <link rel="icon" href="/img/favicon.ico">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="叶尘的技术升级小木屋">
  <meta name="author" content="叶尘">
  <meta name="keywords" content="">
  
  <title>15、数组 - 叶尘的技术升级小木屋</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/shubiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/gundongtiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/daziyanse.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/toubudaziji.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.yechen-blog.cn","root":"/","version":"1.8.11","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>YECHEN</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-qrcode"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/playlist/">
                <i class="iconfont icon-music"></i>
                音乐
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-books"></i>
                链接
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/">
                    
                    主题博客
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/guide/">
                    
                    配置指南
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/icon/">
                    
                    图标用法
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://www.bilibili.com">
                    
                    哔哩哔哩
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/banner.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="15、数组">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      叶尘
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-09-13 22:15" pubdate>
        星期一, 九月 13日 2021, 10:15 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      3.3k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      45
       分钟
    </span>
  

  
  
    
      <!-- LeanCloud 统计文章PV -->
      <span id="leancloud-page-views-container" class="post-meta" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="leancloud-page-views"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">15、数组</h1>
            
            <div class="markdown-body">
              <h3 id="15-1-数组概要"><a href="#15-1-数组概要" class="headerlink" title="15.1 数组概要"></a>15.1 数组概要</h3><ul>
<li>Java 语言中的数组是一种引用数据类型，不属于基本数据类型，数组的父类是 Object</li>
<li><strong>数组实际上是一个容器</strong>，可以同时容纳多个元素</li>
<li>数组中既可以存储基本数据类型数据，也可以存储引用数据类型数据</li>
<li>数组对象存储在堆内存中</li>
<li><strong>数组当中如果存储的是 “引用数据类型”，实际上存储的是对象的 “引用” （对象的内存地址）</strong></li>
<li>==数组一旦创建，在 Java 中规定，数组长度不可变。==访问元素时，如果下标越界，会出现异常 ArrayIndexOutOfBoundsException（数组下标越界异常）</li>
<li>数组的内存结构</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/2021020821085041.png" srcset="/img/loading.gif" lazyload alt="image-20210208210831787"></p>
<ul>
<li><p>==所有数组对象都有一个 “length” 属性（Java自带的），用来获得数组中元素的个数。==</p>
</li>
<li><p>数组存储的元素类型统一。</p>
</li>
<li><p>数组中每一个元素存储的内存地址是连续的。</p>
</li>
<li><p>==将数组中第一个元素的内存地址作为整个数组对象的内存地址。==</p>
</li>
<li><p>数组元素的下标从 0 开始，最后一个元素的下标是： length- 1</p>
</li>
<li><p>数组这种数据结构的优点和缺点：</p>
<ul>
<li><p>优点：查询/检索某个下标的元素速度极快</p>
<ul>
<li>因为每一个元素的内存地址在空间存储上是连续的</li>
<li>同时每一元素所占用的空间大小都是一样的</li>
<li>指导了第一个元素的地址和要查询的元素的下标，就可以通过数学表达式计算出元素的地址，通过地址直接访问该元素。</li>
</ul>
</li>
<li><p>缺点：</p>
<ol>
<li>为了保证数组中每个元素内存地址连续，所以在数组上删除元素的时候，要统一将后面的元素向前或向后位移，效率较低。</li>
<li>不能存储大数据量：因为很难找到一块特别大的连续的内存空间。</li>
</ol>
</li>
</ul>
</li>
</ul>
<h3 id="15-2-一维数组"><a href="#15-2-一维数组" class="headerlink" title="15.2 一维数组"></a>15.2 一维数组</h3><h4 id="15-2-1-声明一维数组"><a href="#15-2-1-声明一维数组" class="headerlink" title="15.2.1 声明一维数组"></a>15.2.1 声明一维数组</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">int</span>[] array01;<br><span class="hljs-keyword">double</span>[] array02;<br>String[] array03;<br><span class="hljs-keyword">boolean</span>[] array04;<br>Object[] array05;<br></code></pre></td></tr></table></figure>

<h4 id="15-2-2-初始化一维数组"><a href="#15-2-2-初始化一维数组" class="headerlink" title="15.2.2 初始化一维数组"></a>15.2.2 初始化一维数组</h4><ul>
<li>静态初始化：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">int</span>[] array = &#123;<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>&#125;;<br></code></pre></td></tr></table></figure>

<ul>
<li>动态初始化：数组长度为 5，每个数组元素元素的值为 0</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">int</span>[] array = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-number">5</span>];<span class="hljs-comment">// 初始化5个长度的int类型数组，每个元素默认值为0</span><br>String[] array2 = <span class="hljs-keyword">new</span> String[<span class="hljs-number">6</span>];<span class="hljs-comment">// 初始哈6个长度的String类型数组，每个元素默认值null</span><br></code></pre></td></tr></table></figure>

<ul>
<li><p>什么时候静态初始化，什么时候动态初始化：</p>
<ul>
<li>当数组中的元素已经确定下来的时候，使用静态初始化。</li>
<li>当数组中元素暂时不能确定下来的时候，动态初始化先把空间占了，之后在赋值。</li>
</ul>
</li>
<li><p>方法的参数是数组</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        String[] array = <span class="hljs-keyword">new</span> String[<span class="hljs-number">5</span>];<br>        String[] array02 = &#123;<span class="hljs-string">&quot;fff&quot;</span>, <span class="hljs-string">&quot;sss&quot;</span>, <span class="hljs-string">&quot;vvv&quot;</span>&#125;;<br>        <span class="hljs-comment">// 向数组中传一个动态初始化的数组</span><br>        print(<span class="hljs-keyword">new</span> String[<span class="hljs-number">6</span>]);<br>        <span class="hljs-comment">// 向数组中传一个静态初始化的数组</span><br>        print(<span class="hljs-keyword">new</span> String[]&#123;<span class="hljs-string">&quot;12&quot;</span>, <span class="hljs-string">&quot;34&quot;</span>, <span class="hljs-string">&quot;56&quot;</span>&#125;);<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">print</span><span class="hljs-params">(String[] array)</span> </span>&#123;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt;= array.length - <span class="hljs-number">1</span>; i++) &#123;<br>            System.out.println(array[i]);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h4 id="15-2-3-mian方法中的String数组"><a href="#15-2-3-mian方法中的String数组" class="headerlink" title="15.2.3 mian方法中的String数组"></a>15.2.3 mian方法中的String数组</h4><p>JVM调用 mian 方法的时候，会自动传一个 String 类型的数组过来</p>
<p><strong>这个数组对象的长度是：0</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;JVM传递过来的数组的长度是：&quot;</span> + args.length);<span class="hljs-comment">// 输出：JVM传递过来的数组的长度是：0</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>==<strong>其实这个数组是留给用户的，用户可以在控制台上输入参数，这个参数自动会被转换为“String[] args”</strong>==</p>
<p>如在 cmd 窗口输入<code> java Test abc def ghi</code></p>
<p>JVM会自动将字符串 “abc def ghi” 按照空格分离，组成一个字符串</p>
<p>此时</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">System.out.println(<span class="hljs-string">&quot;JVM传递过来的数组的长度是：&quot;</span> + args.length);<span class="hljs-comment">// 输出：JVM传递过来的数组的长度是：3</span><br></code></pre></td></tr></table></figure>

<ul>
<li>编历 args数组</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt;= args.length - <span class="hljs-number">1</span> ;i++) &#123;<br>    System.out.println(<span class="hljs-string">&quot;args[&quot;</span> + i + <span class="hljs-string">&quot;] = &quot;</span> + args[i]);<br>&#125;<br><span class="hljs-comment">/*</span><br><span class="hljs-comment">	输出：</span><br><span class="hljs-comment">		args[0] = abc</span><br><span class="hljs-comment">        args[1] = def</span><br><span class="hljs-comment">        args[2] = ghi</span><br><span class="hljs-comment">*/</span><br></code></pre></td></tr></table></figure>

<ul>
<li>使用 IDEA 给 args 数组传参数</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/20210209111158879.png" srcset="/img/loading.gif" lazyload alt="image-20210209110849937"> </p>
<p><img src="https://img-blog.csdnimg.cn/20210209111445177.png" srcset="/img/loading.gif" lazyload alt="image-20210209111435213"></p>
<h4 id="15-2-4-数组扩容"><a href="#15-2-4-数组扩容" class="headerlink" title="15.2.4 数组扩容"></a>15.2.4 数组扩容</h4><p><strong>java中对数组的扩容是：先新建一个大容量的数组，然后将小容量数组中的数据一个一个拷贝到大数组当中。</strong></p>
<p>Java 的 <strong>==System 类中有一个静态方法 arraycopy( )==</strong> ，可以进行数组扩容。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">arraycopy(Object src, <span class="hljs-keyword">int</span> srcPos, Object dest, <span class="hljs-keyword">int</span> destPos, <span class="hljs-keyword">int</span> length)<br></code></pre></td></tr></table></figure>

<ul>
<li>Object src：拷贝源</li>
<li>int srcPos：源数组元素的起始位置</li>
<li>Object dest：目标数组</li>
<li>int destPos：目标数组元素的起始位置</li>
<li>int length：拷贝到元素的个数</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        <span class="hljs-keyword">int</span>[] arr = &#123;<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>&#125;;<span class="hljs-comment">// 拷贝源</span><br>        <span class="hljs-keyword">int</span>[] arr2 = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-number">10</span>];<span class="hljs-comment">// 目标数组1</span><br>        <span class="hljs-keyword">int</span>[] arr3 = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-number">10</span>];<span class="hljs-comment">// 目标数组2</span><br>        <span class="hljs-comment">// 将 arr数组中从下标为 2的元素开始，拷贝 3个元素到 arr2数组，从下标为 3的位置开始放</span><br>        System.arraycopy(arr, <span class="hljs-number">2</span>, arr2, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>);<br>        <span class="hljs-comment">// 将 arr数组中的所用元素拷贝到 arr3数组中</span><br>        System.arraycopy(arr, <span class="hljs-number">0</span>, arr3, <span class="hljs-number">0</span>, arr.length);<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; arr2.length; i++) &#123;<br>            System.out.println(arr2[i]);<br>        &#125;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; arr3.length; i++) &#123;<br>            System.out.println(arr3[i]);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="15-3-二维数组"><a href="#15-3-二维数组" class="headerlink" title="15.3  二维数组"></a>15.3  二维数组</h3><p>==<strong>二维数组就是一个特殊的一维数组，每一个元素都是一个一维数组。</strong>==</p>
<ul>
<li>静态初始化</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">int</span>[][] array = &#123;<br>    &#123;<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>&#125;,<br>    &#123;<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>&#125;,<br>    &#123;<span class="hljs-number">9</span>,<span class="hljs-number">10</span>&#125;<br>&#125;;<br><span class="hljs-comment">// 其中</span><br>System.out.println(array.length);<span class="hljs-comment">// 3</span><br>System.out.println(array[<span class="hljs-number">0</span>].length);<span class="hljs-comment">// 4</span><br>System.out.println(array[<span class="hljs-number">1</span>].length);<span class="hljs-comment">// 3</span><br>System.out.println(array[<span class="hljs-number">2</span>].length);<span class="hljs-comment">// 2</span><br><span class="hljs-comment">// 取出第一个一维数组</span><br><span class="hljs-keyword">int</span>[] array01 = array[<span class="hljs-number">0</span>];<br><span class="hljs-comment">// 	取出第一个一维数组中的第一个元素</span><br><span class="hljs-keyword">int</span> elem11 = array[<span class="hljs-number">0</span>][<span class="hljs-number">0</span>]; <span class="hljs-comment">// elem11 = 1</span><br></code></pre></td></tr></table></figure>

<ul>
<li>动态初始化</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">int</span>[][] array2= <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-number">3</span>][<span class="hljs-number">4</span>];<span class="hljs-comment">// 三个一维数组，每个一维数组中4个元素</span><br></code></pre></td></tr></table></figure>



<h3 id="15-4-冒泡排序算法"><a href="#15-4-冒泡排序算法" class="headerlink" title="15.4 冒泡排序算法"></a>15.4 冒泡排序算法</h3><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Rx411876f?p=577">视频讲解</a></p>
<ul>
<li>冒泡排序的过程</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs java">参与比较的数据：<span class="hljs-number">9</span> <span class="hljs-number">8</span> <span class="hljs-number">10</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">11</span><br><br>第<span class="hljs-number">1</span>次循环：<br><span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">10</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">11</span> (第<span class="hljs-number">1</span>次比较：交换)<br><span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">10</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">11</span> (第<span class="hljs-number">2</span>次比较：不交换)<br><span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">7</span> <span class="hljs-number">10</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">11</span> (第<span class="hljs-number">3</span>次比较：交换)<br><span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">10</span> <span class="hljs-number">0</span> <span class="hljs-number">11</span> (第<span class="hljs-number">4</span>次比较：交换)<br><span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">10</span> <span class="hljs-number">11</span> (第<span class="hljs-number">5</span>次比较：交换)<br><span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">10</span> <span class="hljs-number">11</span> (第<span class="hljs-number">6</span>次比较：不交换)<br>最终冒出的最大数据在右边：<span class="hljs-number">11</span><br><br>参与比较的数据：<span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">10</span><br>第<span class="hljs-number">2</span>次循环：<br><span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">10</span>（第<span class="hljs-number">1</span>次比较：不交换）<br><span class="hljs-number">8</span> <span class="hljs-number">7</span> <span class="hljs-number">9</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">10</span>（第<span class="hljs-number">2</span>次比较：交换）<br><span class="hljs-number">8</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">9</span> <span class="hljs-number">0</span> <span class="hljs-number">10</span>（第<span class="hljs-number">3</span>次比较：交换）<br><span class="hljs-number">8</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">9</span> <span class="hljs-number">10</span>（第<span class="hljs-number">4</span>次比较：交换）<br><span class="hljs-number">8</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">9</span> <span class="hljs-number">10</span>（第<span class="hljs-number">5</span>次比较：不交换）<br><br>参与比较的数据：<span class="hljs-number">8</span> <span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">9</span><br>第<span class="hljs-number">3</span>次循环：<br><span class="hljs-number">7</span> <span class="hljs-number">8</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">9</span>（第<span class="hljs-number">1</span>次比较：交换）<br><span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">8</span> <span class="hljs-number">0</span> <span class="hljs-number">9</span>（第<span class="hljs-number">2</span>次比较：交换）<br><span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">8</span> <span class="hljs-number">9</span>（第<span class="hljs-number">3</span>次比较：交换）<br><span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">8</span> <span class="hljs-number">9</span>（第<span class="hljs-number">4</span>次比较：不交换）<br><br>参与比较的数据：<span class="hljs-number">7</span> <span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">8</span><br>第<span class="hljs-number">4</span>次循环：<br><span class="hljs-number">6</span> <span class="hljs-number">7</span> <span class="hljs-number">0</span> <span class="hljs-number">8</span>（第<span class="hljs-number">1</span>次比较：交换）<br><span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">7</span> <span class="hljs-number">8</span>（第<span class="hljs-number">2</span>次比较：交换）<br><span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">7</span> <span class="hljs-number">8</span>（第<span class="hljs-number">3</span>次比较：不交换）<br><br>参与比较的数据：<span class="hljs-number">6</span> <span class="hljs-number">0</span> <span class="hljs-number">7</span><br>第<span class="hljs-number">5</span>次循环：<br><span class="hljs-number">0</span> <span class="hljs-number">6</span> <span class="hljs-number">7</span>（第<span class="hljs-number">1</span>次比较：交换）<br><span class="hljs-number">0</span> <span class="hljs-number">6</span> <span class="hljs-number">7</span>（第<span class="hljs-number">2</span>次比较：不交换）<br><br>参与比较的数据：<span class="hljs-number">0</span> <span class="hljs-number">6</span><br>第<span class="hljs-number">6</span>次循环：<br><span class="hljs-number">0</span> <span class="hljs-number">6</span> （第<span class="hljs-number">1</span>次比较：不交换）<br><br></code></pre></td></tr></table></figure>

<ul>
<li>自己写一个冒泡排序</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        <span class="hljs-keyword">int</span>[] array = &#123;<span class="hljs-number">2</span>, <span class="hljs-number">56</span>, <span class="hljs-number">66</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">100</span>&#125;;<br>        <span class="hljs-comment">// 每次循环完后冒出最大的元素放最后</span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= array.length-<span class="hljs-number">1</span>; i++) &#123;<br>            <span class="hljs-comment">// 前后两个元素比较，大的往后移</span><br>            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j &lt;= array.length-(i+<span class="hljs-number">1</span>); j++) &#123;<br>                <span class="hljs-keyword">if</span> (array[j] &gt; array[j+<span class="hljs-number">1</span>]) &#123;<br>                    <span class="hljs-keyword">int</span> temp = array[j];<br>                    array[j] = array[j+<span class="hljs-number">1</span>];<br>                    array[j+<span class="hljs-number">1</span>] = temp;<br>                &#125;<br>            &#125;<br>        &#125;<br><br>        <span class="hljs-comment">// 另一种写法</span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = array.length - <span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">1</span>; i--) &#123;<br>            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j &lt; i; j++) &#123;<br>                <span class="hljs-keyword">if</span> (array[j] &gt; array[j+<span class="hljs-number">1</span>]) &#123;<br>                    <span class="hljs-keyword">int</span> temp = array[j];<br>                    array[j] = array[j+<span class="hljs-number">1</span>];<br>                    array[j+<span class="hljs-number">1</span>] = temp;<br>                &#125;<br>            &#125;<br>        &#125;<br>        <br>        <br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; array.length; i++) &#123;<br>            System.out.println(array[i]);<br>        &#125;<br><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="15-5-选择排序算法"><a href="#15-5-选择排序算法" class="headerlink" title="15.5 选择排序算法"></a>15.5 选择排序算法</h3><ul>
<li>原理：<strong>每一次从这堆参与比较的数据中找出最小值，那这个最小值和最前面的元素交换位置。</strong> </li>
<li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Rx411876f?p=579">视频讲解</a></li>
<li>自己写的一个排序算法</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        <span class="hljs-keyword">int</span>[] array = &#123;<span class="hljs-number">2</span>, <span class="hljs-number">56</span>, <span class="hljs-number">66</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">100</span>&#125;;<br>        <span class="hljs-comment">// 分析一下</span><br>        <span class="hljs-comment">// 当前数组有7个元素</span><br>        <span class="hljs-comment">// 第【1】次 循环找到1是最小的，放到第【1】个位置，剩下【6】个数比较</span><br>        <span class="hljs-comment">// 第【2】次 循环找到2是最小的，放到第【2】个位置，剩下【5】个数比较</span><br>        <span class="hljs-comment">// 第【3】次 循环找到3是最小的，放到第【3】个位置，剩下【4】个数比较</span><br>        <span class="hljs-comment">// ......</span><br>        <span class="hljs-comment">// 第【6】次 循环找到66是最小的，放到第【6】个位置，循环结束。</span><br><br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; array.length-<span class="hljs-number">1</span>; i++) &#123;<br>            <span class="hljs-comment">// 先默认下标为 i的元素是最小的</span><br>            <span class="hljs-keyword">int</span> minNumIndex = i;<br>            <span class="hljs-comment">// 下标为 i的元素与其之后的元素一一比较</span><br>            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i + <span class="hljs-number">1</span>; j &lt; array.length; j++) &#123;<br>                <span class="hljs-comment">// 如果最小元素大于之后的元素</span><br>                <span class="hljs-keyword">if</span> (array[minNumIndex] &gt; array[j]) &#123;<br>                    <span class="hljs-comment">// 就将更小的元素的下标赋给 minNumIndex</span><br>                    minNumIndex = j;<br>                &#125;<br>            &#125;<br>            <span class="hljs-comment">// 如果 minNumIndex不等于默认的 i，就将第 i个元素和最小元素进行交换</span><br>            <span class="hljs-keyword">if</span> (minNumIndex != i) &#123;<br>                <span class="hljs-keyword">int</span> temp = array[i];<br>                array[i] = array[minNumIndex];<br>                array[minNumIndex] = temp;<br>            &#125;<br>        &#125;<br><br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; array.length; i++) &#123;<br>            System.out.println(array[i]);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="15-6-二分法查找"><a href="#15-6-二分法查找" class="headerlink" title="15.6 二分法查找"></a>15.6 二分法查找</h3><p><strong>二分查找算法是基于排序好的算法的。</strong></p>
<ul>
<li>自己写的二分法查找</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">  * 从数组中查找目标元素的下标。</span><br><span class="hljs-comment">  * <span class="hljs-doctag">@param</span> arr 被查找的数组（这个必须是已经排序的。）</span><br><span class="hljs-comment">  * <span class="hljs-doctag">@param</span> dest 目标元素</span><br><span class="hljs-comment">  * <span class="hljs-doctag">@return</span> -1表示该元素不存在，其它表示返回该元素的下标。</span><br><span class="hljs-comment">  */</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">binarySearch</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] arr, <span class="hljs-keyword">int</span> dest)</span> </span>&#123;<br>    <span class="hljs-comment">// 开始下标</span><br>    <span class="hljs-keyword">int</span> begin = <span class="hljs-number">0</span>;<br>    <span class="hljs-comment">// 结束下标</span><br>    <span class="hljs-keyword">int</span> end = arr.length - <span class="hljs-number">1</span>;<br>    <span class="hljs-comment">// 开始元素的下标只要在结束元素下标的左边，就有机会继续循环。</span><br>    <span class="hljs-keyword">while</span>(begin &lt;= end) &#123;<br>        <span class="hljs-comment">// 中间元素下标</span><br>        <span class="hljs-keyword">int</span> mid = (begin + end) / <span class="hljs-number">2</span>;<br>        <span class="hljs-keyword">if</span> (arr[mid] == dest) &#123;<br>            <span class="hljs-keyword">return</span> mid;<br>        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (arr[mid] &lt; dest) &#123;<br>            <span class="hljs-comment">// 目标在“中间”的右边</span><br>            <span class="hljs-comment">// 开始元素下标需要发生变化（开始元素的下标需要重新赋值）</span><br>            begin = mid + <span class="hljs-number">1</span>; <span class="hljs-comment">// 一直增</span><br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-comment">// arr[mid] &gt; dest</span><br>            <span class="hljs-comment">// 目标在“中间”的左边</span><br>            <span class="hljs-comment">// 修改结束元素的下标</span><br>            end = mid - <span class="hljs-number">1</span>; <span class="hljs-comment">// 一直减</span><br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>综合以下先排序后查找</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.yechen.java;<br><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test06</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        <span class="hljs-keyword">int</span>[] array = &#123;<span class="hljs-number">2</span>, <span class="hljs-number">56</span>, <span class="hljs-number">66</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">100</span>&#125;;<br>        Num[] nums = <span class="hljs-keyword">new</span> Num[<span class="hljs-number">7</span>];<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; nums.length; i++) &#123;<br>            nums[i] = <span class="hljs-keyword">new</span> Num(array[i], i);<br>        &#125;<br><br>        print(nums);<br>        System.out.println(<span class="hljs-string">&quot;==========&quot;</span>);<br>        selectSort(nums);<br>        print(nums);<br>        System.out.println(<span class="hljs-string">&quot;==========&quot;</span>);<br>        <span class="hljs-keyword">int</span> indexAfterSort = binarySearch(nums, <span class="hljs-number">66</span>);<br>        <span class="hljs-keyword">int</span> index = nums[indexAfterSort].getIndex();<br>        System.out.println(<span class="hljs-string">&quot;66在排序之后的下标为：&quot;</span> + indexAfterSort);<br>        System.out.println(<span class="hljs-string">&quot;66原下标为：&quot;</span> + index);<br>    &#125;<br><br>    <span class="hljs-comment">// 选择排序算法</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">selectSort</span><span class="hljs-params">(Object[] array)</span> </span>&#123;<br>        <span class="hljs-comment">// 向下转型，这里就不用 instanceof 判断了</span><br>        Num[] arrays = (Num[]) array;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; arrays.length - <span class="hljs-number">1</span>; i++) &#123;<br>            <span class="hljs-comment">// 先默认下标为 i的元素是最小的</span><br>            <span class="hljs-keyword">int</span> minNumIndex = i;<br>            <span class="hljs-comment">// 下标为 i的元素与其之后的元素一一比较</span><br>            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i + <span class="hljs-number">1</span>; j &lt; arrays.length; j++) &#123;<br>                <span class="hljs-comment">// 如果最小元素大于之后的元素</span><br>                <span class="hljs-keyword">if</span> (arrays[minNumIndex].getNum() &gt; arrays[j].getNum()) &#123;<br>                    <span class="hljs-comment">// 就将更小的元素的下标赋给 minNumIndex</span><br>                    minNumIndex = j;<br>                &#125;<br>            &#125;<br>            <span class="hljs-comment">// 如果 minNumIndex不等于默认的 i，就将第 i个元素和最小元素进行交换</span><br>            <span class="hljs-keyword">if</span> (minNumIndex != i) &#123;<br>                <span class="hljs-comment">/*int temp = arrays[i].getNum();</span><br><span class="hljs-comment">                arrays[i].setNum(arrays[minNumIndex].getNum());</span><br><span class="hljs-comment">                arrays[minNumIndex].setNum(temp);</span><br><span class="hljs-comment">                int tempIndex = arrays[i].getIndex();</span><br><span class="hljs-comment">                arrays[i].setIndex(arrays[minNumIndex].getIndex());</span><br><span class="hljs-comment">                arrays[minNumIndex].setIndex(tempIndex);*/</span><br>                Num tempNum = arrays[i];<br>                arrays[i] = arrays[minNumIndex];<br>                arrays[minNumIndex] = tempNum;<br>            &#125;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">// 二分法查找</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">binarySearch</span><span class="hljs-params">(Object[] array, <span class="hljs-keyword">int</span> dest)</span> </span>&#123;<br>        <span class="hljs-comment">// 向下转型</span><br>        Num[] arr = (Num[]) array;<br>        <span class="hljs-comment">// 开始下标</span><br>        <span class="hljs-keyword">int</span> begin = <span class="hljs-number">0</span>;<br>        <span class="hljs-comment">// 结束下标</span><br>        <span class="hljs-keyword">int</span> end = arr.length - <span class="hljs-number">1</span>;<br>        <span class="hljs-comment">// 开始元素的下标只要在结束元素下标的左边，就有机会继续循环。</span><br>        <span class="hljs-keyword">while</span>(begin &lt;= end) &#123;<br>            <span class="hljs-comment">// 中间元素下标</span><br>            <span class="hljs-keyword">int</span> mid = (begin + end) / <span class="hljs-number">2</span>;<br>            <span class="hljs-keyword">if</span> (arr[mid].getNum() == dest) &#123;<br>                <span class="hljs-keyword">return</span> mid;<br>            &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (arr[mid].getNum() &lt; dest) &#123;<br>                <span class="hljs-comment">// 目标在“中间”的右边</span><br>                <span class="hljs-comment">// 开始元素下标需要发生变化（开始元素的下标需要重新赋值）</span><br>                begin = mid + <span class="hljs-number">1</span>; <span class="hljs-comment">// 一直增</span><br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                <span class="hljs-comment">// arr[mid] &gt; dest</span><br>                <span class="hljs-comment">// 目标在“中间”的左边</span><br>                <span class="hljs-comment">// 修改结束元素的下标</span><br>                end = mid - <span class="hljs-number">1</span>; <span class="hljs-comment">// 一直减</span><br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>    &#125;<br><br>    <span class="hljs-comment">// 编历数组</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">print</span><span class="hljs-params">(Object[] arr)</span> </span>&#123;<br>        <span class="hljs-comment">// 向下转型</span><br>        Num[] nums = (Num[]) arr;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; nums.length; i++) &#123;<br>            System.out.println(nums[i].getNum() +<span class="hljs-string">&quot;,&quot;</span>+ nums[i].getIndex());<br>        &#125;<br>    &#125;<br>&#125;<br><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Num</span> </span>&#123;<br>    <span class="hljs-comment">// 保存的值</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> num;<br>    <span class="hljs-comment">// 在数组中的下标</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> index;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Num</span><span class="hljs-params">()</span> </span>&#123;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Num</span><span class="hljs-params">(<span class="hljs-keyword">int</span> num, <span class="hljs-keyword">int</span> index)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.num = num;<br>        <span class="hljs-keyword">this</span>.index = index;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getNum</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> num;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setNum</span><span class="hljs-params">(<span class="hljs-keyword">int</span> num)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.num = num;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getIndex</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> index;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setIndex</span><span class="hljs-params">(<span class="hljs-keyword">int</span> index)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.index = index;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p>输出</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-number">2</span>,<span class="hljs-number">0</span><br><span class="hljs-number">56</span>,<span class="hljs-number">1</span><br><span class="hljs-number">66</span>,<span class="hljs-number">2</span><br><span class="hljs-number">1</span>,<span class="hljs-number">3</span><br><span class="hljs-number">4</span>,<span class="hljs-number">4</span><br><span class="hljs-number">3</span>,<span class="hljs-number">5</span><br><span class="hljs-number">100</span>,<span class="hljs-number">6</span><br>==========<br><span class="hljs-number">1</span>,<span class="hljs-number">3</span><br><span class="hljs-number">2</span>,<span class="hljs-number">0</span><br><span class="hljs-number">3</span>,<span class="hljs-number">5</span><br><span class="hljs-number">4</span>,<span class="hljs-number">4</span><br><span class="hljs-number">56</span>,<span class="hljs-number">1</span><br><span class="hljs-number">66</span>,<span class="hljs-number">2</span><br><span class="hljs-number">100</span>,<span class="hljs-number">6</span><br>==========<br><span class="hljs-number">66</span>在排序之后的下标为：<span class="hljs-number">5</span><br><span class="hljs-number">66</span>原下标为：<span class="hljs-number">2</span><br></code></pre></td></tr></table></figure>

<h3 id="15-7-Arrays工具类的使用（数组工具类）"><a href="#15-7-Arrays工具类的使用（数组工具类）" class="headerlink" title="15.7 Arrays工具类的使用（数组工具类）"></a>15.7 Arrays工具类的使用（数组工具类）</h3><p><strong>使用的时候查阅 API 文档即可。</strong></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/Java-%E5%9F%BA%E7%A1%80/">Java 基础</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Java-%E5%9F%BA%E7%A1%80/">Java 基础</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/09/13/Java%E5%9F%BA%E7%A1%80-2021-09-13-%E5%B8%B8%E7%94%A8%E7%B1%BB/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">16、常用类</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/09/13/Java%E5%9F%BA%E7%A1%80-2021-09-13-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/">
                        <span class="hidden-mobile">14、面向对象</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/js/local-search.js" ></script>






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>














  
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxuehua.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/caidai.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/love.js"></script>
<script src="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>

  <!--动态线条背景-->
  <script type="text/javascript" color="70,119,184" opacity='1' zIndex="-2" count="200" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/koharu.model.json"},"display":{"position":"left","width":200,"height":400},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
